package com.example.casper.Experiment2024.View;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.NonNull;
import com.example.casper.Experiment2024.R;

import java.util.Calendar;

public class CustomClockView extends View {
    private Paint paint;
    private int width;
    private int height;
    private float hour;
    private float minute;
    private float second;

    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            invalidate(); // 刷新视图
            handler.postDelayed(this, 1000); // 每秒更新一次
        }
    };

    public CustomClockView(Context context) {
        super(context);
        init();
    }

    public CustomClockView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public void startUpdating() {
        handler.post(runnable); // 启动更新
    }

    public void stopUpdating() {
        handler.removeCallbacks(runnable);
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
    }

    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        handler.removeCallbacks(runnable); // 停止更新
    }

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawClockFace(canvas);
        drawClockHands(canvas);
    }

    private void drawClockFace(Canvas canvas) {
        // 绘制表盘相关代码基本不变，这里省略详细注释，和之前类似
        paint.setColor(Color.BLACK);
        canvas.drawCircle(width / 2, height / 2, Math.min(width, height) / 2 - 20, paint);

        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(5);
        canvas.drawCircle(width / 2, height / 2, Math.min(width, height) / 2 - 30, paint);

        paint.setColor(Color.BLACK);
        paint.setTextSize(50);
        for (int i = 0; i < 12; i++) {
            float angle = (float) (Math.toRadians(i * 30));
            float x = (float) (width / 2 + (Math.min(width, height) / 2 - 60) * Math.sin(angle));
            float y = (float) (height / 2 - (Math.min(width, height) / 2 - 60) * Math.cos(angle));
            canvas.drawText(String.valueOf(i == 0? 12 : i), x - 10, y + 10, paint);
        }
    }

    private void drawClockHands(Canvas canvas) {
        Calendar calendar = Calendar.getInstance();
        hour = calendar.get(Calendar.HOUR);
        minute = calendar.get(Calendar.MINUTE);
        second = calendar.get(Calendar.SECOND);

        // 绘制时针（使用图片替换原来绘制直线的方式）
        drawHourHandImage(canvas);

        // 绘制分针（使用图片替换原来绘制直线的方式）
        drawMinuteHandImage(canvas);

        // 绘制秒针
        drawSecondHandImage(canvas);
    }

    private void drawHourHandImage(Canvas canvas) {
        // 从资源中加载时针图片
        Bitmap hourHandBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.hour_handle);
        int bitmapWidth = hourHandBitmap.getWidth();
        int bitmapHeight = hourHandBitmap.getHeight();

        // 根据时针角度计算旋转角度并创建旋转矩阵
        Matrix matrix = new Matrix();
        float hourAngle = (float) ((hour + minute / 60) * 30 + 120);
        matrix.postRotate(hourAngle, bitmapWidth / 2, bitmapHeight / 2);

        // 对图片进行缩放操作
        float scaleFactor = 1.5f;  // 这里将图片放大1.5倍，你可以根据需要修改这个值
        matrix.postScale(scaleFactor, scaleFactor, bitmapWidth / 2, bitmapHeight / 2);

        // 对图片进行旋转和缩放操作
        Bitmap rotatedAndScaledHourHandBitmap = Bitmap.createBitmap(hourHandBitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, true);

        // 重新计算图片绘制的坐标位置，使其位于表盘中心
        int x = width / 2 - rotatedAndScaledHourHandBitmap.getWidth() / 2;
        int y = height / 2 - rotatedAndScaledHourHandBitmap.getHeight() / 2;

        // 将旋转和缩放后的图片绘制到画布上
        canvas.drawBitmap(rotatedAndScaledHourHandBitmap, x, y, paint);
    }

    private void drawMinuteHandImage(Canvas canvas) {
        // 从资源中加载分针图片
        Bitmap minuteHandBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.minute_handle);
        int bitmapWidth = minuteHandBitmap.getWidth();
        int bitmapHeight = minuteHandBitmap.getHeight();

        // 根据分针角度计算旋转角度并创建旋转矩阵
        Matrix matrix = new Matrix();
        float minuteAngle = (float) ((minute + second / 60) * 6 + 120);
        matrix.postRotate(minuteAngle, bitmapWidth / 2, bitmapHeight / 2);

        float scaleFactor = 1.65f;
        matrix.postScale(scaleFactor, scaleFactor, bitmapWidth / 2, bitmapHeight / 2);
        // 对图片进行旋转操作
        Bitmap rotatedMinuteHandBitmap = Bitmap.createBitmap(minuteHandBitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, true);

        // 计算图片绘制的坐标位置，使其位于表盘中心
        int x = width / 2 - rotatedMinuteHandBitmap.getWidth() / 2;
        int y = height / 2 - rotatedMinuteHandBitmap.getHeight() / 2;

        // 将旋转后的图片绘制到画布上
        canvas.drawBitmap(rotatedMinuteHandBitmap, x, y, paint);
    }

    private void drawSecondHandImage(Canvas canvas) {
        // 从资源中加载秒针图片
        Bitmap secondHandBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.second_handle);
        int bitmapWidth = secondHandBitmap.getWidth();
        int bitmapHeight = secondHandBitmap.getHeight();

        // 根据秒针角度计算旋转角度并创建旋转矩阵
        Matrix matrix = new Matrix();
        float secondAngle = (float) (second * 6 + 120);
        matrix.postRotate(secondAngle, bitmapWidth / 2, bitmapHeight / 2);

        float scaleFactor = 2.0f;
        matrix.postScale(scaleFactor, scaleFactor, bitmapWidth / 2, bitmapHeight / 2);
        // 对图片进行旋转操作
        Bitmap rotatedSecondHandBitmap = Bitmap.createBitmap(secondHandBitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, true);

        // 计算图片绘制的坐标位置，使其位于表盘中心
        int x = width / 2 - rotatedSecondHandBitmap.getWidth() / 2;
        int y = height / 2 - rotatedSecondHandBitmap.getHeight() / 2;

        // 将旋转后的图片绘制到画布上
        canvas.drawBitmap(rotatedSecondHandBitmap, x, y, paint);
    }
}
